<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="会员昵称">
        <el-input v-model="queryParams.keyword" placeholder="会员昵称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="用户名"></el-table-column>
      <el-table-column prop="mobile" header-align="center" align="center" label="手机号"></el-table-column>
      <el-table-column prop="avatar" header-align="center" align="center" label="头像">
        <template slot-scope="scope">
          <image-preview :width="50" :height="50" :src="scope.row.avatar"/>
        </template>
      </el-table-column>
      <el-table-column prop="integral" header-align="center" align="center" label="积分"></el-table-column>
      <el-table-column prop="growth" header-align="center" align="center" label="成长值"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            @change="handleStatusChange(scope.row)"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="注册时间"></el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />
  </div>
</template>

<script>
import {memberList, updateMember} from "@/api/member/member";

export default {
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      dataList: [],
      total: 0,
      loading: false,
      selections: [],
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    // 获取数据列表
    loadData() {
      this.loading = true
      memberList(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records
          this.total = res.data.total
        }
        this.loading = false
      })
    },

    handleStatusChange(row) {
      let data = {
        id: row.id,
        status: row.status
      }
      updateMember(data).then(res => {
        if (res.success) {
          this.$message.success("状态修改成功")
          this.loadData()
        }
      })
    },

    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      }
      this.loadData()
    },
    // 多选
    selectionChangeHandle(val) {
      this.selections = val;
    }
  }
};
</script>
